Page({
    data: {
        list: [],
        activeIndex: 0,
        tops: [],
        activeCid: "",
        cart: [],     //购物车
        tag: false,      //cart组件是否显示
    },
    async onLoad() {
        try {
            let result = await wx.cloud.callFunction({ name: "list" })
            this.setData({
                list: result.result.data
            })

            //获取右侧每一个大块的高度，放到一个数组
            let query = wx.createSelectorQuery()
            query.selectAll('.item').boundingClientRect()
            query.exec(res => {
                let tops = res[0].map(item => {
                    return item.top
                })
                this.setData({
                    tops
                })
            })

        } catch (err) {
            console.log(err)
        }

    },
    async doAdd() {
        let result = await wx.cloud.callFunction({
            name: "list",
            data: { a: 100, b: 200 }
        })
        this.setData({
            list: result.data
        })
    },
    //右侧滚动
    rightScroll(e) {
        // console.log(e)
        let { scrollTop } = e.detail;
        // console.log(scrollTop, this.data.tops[this.data.activeIndex + 1])
        if (scrollTop > this.data.tops[this.data.activeIndex + 1]) {
            this.setData({
                activeIndex: this.data.activeIndex + 1
            })
        }

        console.log(scrollTop)
        if (scrollTop <= this.data.tops[this.data.activeIndex - 1] + 5) {
            this.setData({
                activeIndex: this.data.activeIndex - 1
            })
        }
    },
    // 左侧点击
    leftTap(e) {
        // console.log(e)
        let cid = e.currentTarget.id;
        let index = e.currentTarget.dataset.index;
        this.setData({
            activeCid: cid,
            activeIndex: index
        })
    },
    //点击减少
    dec(e) {
        let { index, subIndex } = e.currentTarget.dataset;
        console.log(index, subIndex)


    },
    inc(e) {
        let { index, subIndex } = e.currentTarget.dataset;
        // console.log(index,subIndex)
        let info = this.data.list[index].data[subIndex];
        //存储到缓存
        let cart = wx.getStorageSync('cart') || []
        let theIndex = cart.findIndex(item => {
            return item._id == info._id
        })

        if (theIndex == -1) {
            cart.unshift(info)
        } else {
            console.log(cart)
            cart[theIndex].quantity += 1;
            console.log(cart)
        }
        wx.setStorageSync('cart', cart)
        //渲染到页面
        this.setData({
            cart: cart
        })
    },
    //显示cart组件
    show() {
        let cart = wx.getStorageSync('cart') || []
        this.setData({
            tag: true,
            cart
        })
    },
    hide(tag) {
        this.setData({
            tag: false
        })

    },
    async pay() {
        //获取openid
        // let openid = wx.getStorageSync('openid')
        let result = await wx.cloud.callFunction({ name: "pay" }).catch(err => console.error(err))
       
        result = await wx.requestPayment({
            ...result.result.payment
        }).catch(err => console.error(err))
       

        //获取预支付信息
        //支付成功：消息推送  打印小票 跳转订单页

    }
})